package com.lwlk.zdk.controller.Menu;

import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.pw.pwMenuMapper;
import com.lwlk.zdk.mapper.pw.pwPlatformRoleMapper;
import com.lwlk.zdk.mapper.pw.pwUserMapper;
import com.lwlk.zdk.model.Encapsulation.EncapsulationMenu;
import com.lwlk.zdk.model.TreeMenu;
import com.lwlk.zdk.model.TreeMenuThree;
import com.lwlk.zdk.model.TreeMenuTwo;
import com.lwlk.zdk.model.pw.pwMenu;
import com.lwlk.zdk.model.pw.pwPlatformRole;
import com.lwlk.zdk.util.Response;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    @Autowired
    pwMenuMapper pwMenuMapper;
    @Autowired
    pwPlatformRoleMapper platformRoleMapper;
    @Autowired
    pwUserMapper pwUserMapper;


    @ApiOperation(value = "添加菜单")
    @RequestMapping(value = "/menuAdd",method = RequestMethod.POST)
    public Response getMenuAdd(@RequestBody pwMenu pwMenu){
      Response response=new Response();
      int i=pwMenuMapper.insertSelective(pwMenu);
      response.setData(i);
      response.setCode(RetCode.SUCCESS);
      response.setMsg("执行成功");
      return response;
    }

    @ApiOperation(value = "修改菜单")
    @RequestMapping(value = "/menuUpdate",method = RequestMethod.POST)
    public Response getUpdate(@RequestBody pwMenu pwMenu, HttpServletRequest request){
        Response response=new Response();
        //获取账号的等级
        Integer level= Integer.valueOf(request.getHeader("level"));
        if (level!=1){
            response.setCode(RetCode.PERMISSION_DENIED);
            return response;
        }
        int i=pwMenuMapper.updateByExampleSelective(pwMenu);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        response.setData(i);
        return response;
    }

    @ApiOperation(value = "删除菜单")
    @RequestMapping(value = "/deleteMenu",method = RequestMethod.DELETE)
    public Response delete(Integer id,HttpServletRequest request){
      Response response=new Response();
        //获取账号的等级
        Integer level= Integer.valueOf(request.getHeader("level"));
        if (level!=1){
            response.setCode(RetCode.PERMISSION_DENIED);
            return response;
        }
        int i=pwMenuMapper.deleteByExample(id);
        response.setData(i);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }

    @ApiOperation(value = "菜单树形展示")
    @RequestMapping(value = "/treeMenu",method = RequestMethod.GET)
    public Response getTreeRole(){
        Response response=new Response();
        //一级菜单
        List<TreeMenu> menuStair=pwMenuMapper.selectMenuStair();
        ArrayList arrayList=new ArrayList();
        Map<Integer, TreeMenu> areaMap = new HashMap<>();
        for (int i = 0; i < menuStair.size(); i++) {
            areaMap.put(menuStair.get(i).getId(),menuStair.get(i));
        }
        for (int i = 0; i < areaMap.size(); i++) {
            Integer id = areaMap.get(i+1).getId();
            //二级菜单
            List<TreeMenuTwo> menuTwo=pwMenuMapper.selectMenuTwo(id);
            for (int j = 0; j < menuTwo.size(); j++) {
                if (areaMap.get(i+1).getId()==menuTwo.get(j).getMenuIevelId()){
                    areaMap.get(id).setChildren(new ArrayList(menuTwo));
                    for (int k = 0; k < menuTwo.size(); k++) {
                        //三级菜单
                        List<TreeMenuThree> pwMenuList=pwMenuMapper.selectThreeMenu(menuTwo.get(k).getId());
                        areaMap.get(id).getChildren().get(k).setChildren(pwMenuList);
                    }
                }
            }
        }
        arrayList.add(areaMap.get(2));
        arrayList.add(areaMap.get(3));
        arrayList.add(areaMap.get(4));
        arrayList.add(areaMap.get(5));
        areaMap.remove(2);
        areaMap.remove(3);
        areaMap.remove(4);
        areaMap.remove(5);
        ArrayList array=new ArrayList(areaMap.values());
        HashMap map=new HashMap();
        map.put("menuList",array);
        map.put("totalList",arrayList);
        response.setCode(RetCode.SUCCESS);
        response.setData(map);
        return response;
    }


    @ApiOperation(value = "角色所属菜单树形展示")
    @RequestMapping(value = "/userMenu",method = RequestMethod.GET)
    public Response getTreeRoleAndUserId(Integer roleId){
        Response response=new Response();
   /*     List<Map<String,Object>> userDetails=pwUserMapper.selectDetails(uId);
        Integer roleId=Integer.parseInt(String.valueOf(userDetails.get(0).get("roleId")));*/
        pwPlatformRole platformRole =platformRoleMapper.selectByPrimaryKey(roleId);
        //一级菜单
        List<TreeMenu> menuStair=pwMenuMapper.selectMenuStair();
        ArrayList arrayList=new ArrayList();
        Map<Integer, TreeMenu> areaMap = new HashMap<>();
        for (int i = 0; i < menuStair.size(); i++) {
            areaMap.put(menuStair.get(i).getId(),menuStair.get(i));
        }
        for (int i = 0; i < areaMap.size(); i++) {
            //一级目录id
            Integer id = areaMap.get(i+1).getId();
            if (platformRole.getMenuIevelId()!=null){
                String[] menuIevelId=platformRole.getMenuIevelId().split(",");
                for (String mId : menuIevelId) {
                    if (Integer.parseInt(mId)==id){
                        areaMap.get(i+1).setExpand(true);
                    }
                }
            }
        }

        for (int i = 0; i < areaMap.size(); i++) {
            Integer id = areaMap.get(i+1).getId();
            //二级菜单
            List<TreeMenuTwo> menuTwo=pwMenuMapper.selectMenuTwo(id);
            for (int j = 0; j < menuTwo.size(); j++) {
                if (areaMap.get(i+1).getId()==menuTwo.get(j).getMenuIevelId()){
                    areaMap.get(id).setChildren(new ArrayList(menuTwo));
                    for (int k = 0; k < menuTwo.size(); k++) {
                        //三级菜单
                        List<TreeMenuThree> pwMenuList=pwMenuMapper.selectThreeMenu(menuTwo.get(k).getId());
                        areaMap.get(id).getChildren().get(k).setChildren(pwMenuList);
                    }
                }
            }
        }
        for (int i = 0; i < areaMap.size(); i++) {
            //二级目录id
            if (areaMap.get(i+1).getChildren()!=null){
                //Integer id = areaMap.get(i+1).getChildren().get(i).getId();
                //二级菜单集合
                List<TreeMenuTwo> TreeTwo=areaMap.get(i+1).getChildren();
                if (platformRole.getSubmenuId()!=null){
                    String[] submenuId=platformRole.getSubmenuId().split(",");
                    for (int j = 0; j < TreeTwo.size(); j++) {
                        Integer id=TreeTwo.get(j).getId();
                        for (String sId : submenuId) {
                            if (Integer.parseInt(sId)==id){
                                areaMap.get(i+1).getChildren().get(j).setExpand(true);
                            }
                        }
                        if (platformRole.getThreeLevelMenuId()!=null){
                            String[] threeLevelMenuId=platformRole.getThreeLevelMenuId().split(",");
                            //三级菜单集合
                            List<TreeMenuThree> TreeThrees=areaMap.get(i+1).getChildren().get(j).getChildren();
                            for (int k = 0; k < TreeThrees.size(); k++) {
                                Integer tId = TreeThrees.get(k).getId();
                                for (String threeId : threeLevelMenuId) {
                                    if (Integer.parseInt(threeId)==tId){
                                        areaMap.get(i+1).getChildren().get(j).getChildren().get(k).setExpand(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        arrayList.add(areaMap.get(2));
        arrayList.add(areaMap.get(3));
        arrayList.add(areaMap.get(4));
        arrayList.add(areaMap.get(5));
        areaMap.remove(2);
        areaMap.remove(3);
        areaMap.remove(4);
        areaMap.remove(5);
        ArrayList array=new ArrayList(areaMap.values());
        HashMap map=new HashMap();
        map.put("menuList",array);
        map.put("totalList",arrayList);
        response.setCode(RetCode.SUCCESS);
        response.setData(map);
        return response;
    }


    @ApiOperation(value = "展示菜单")
    @RequestMapping(value = "/showMenu",method = RequestMethod.GET)
    public Response getShowMenu(pwMenu pwMenu){
     Response response=new Response();
     Map<String,Object>encapsulationMenu=new HashMap<>();
     EncapsulationMenu totallist = new EncapsulationMenu();
     pwMenu pwMenu1=new pwMenu();
     pwMenu pwMenu2=new pwMenu();
     pwMenu pwMenu3=new pwMenu();
     List<pwMenu> arrayList=new ArrayList();
        List<com.lwlk.zdk.model.pw.pwMenu> menuList;
        if (pwMenu.getId()==null){
            menuList = pwMenuMapper.selectMenuIevelSign();
         for (int i = 0; i < menuList.size(); i++) {
              Integer id =menuList.get(i).getId();
              if (id==3){
                  pwMenu1.setId(id);
                  pwMenu1.setMenuIevelId(menuList.get(i).getMenuIevelId());
                  pwMenu1.setMenuIevelSign(menuList.get(i).getMenuIevelSign());
                  pwMenu1.setRoutingPath(menuList.get(i).getRoutingPath());
                  pwMenu1.setSubmenuId(menuList.get(i).getSubmenuId());
                  pwMenu1.setSubmenuSign(menuList.get(i).getSubmenuSign());
                  pwMenu1.setThreeLevelMenuId(menuList.get(i).getThreeLevelMenuId());
                  pwMenu1.setThreeLevelMenuSign(menuList.get(i).getThreeLevelMenuSign());
                  arrayList.add(pwMenu1);
              }
             if (id==4){
                 pwMenu2.setId(id);
                 pwMenu2.setMenuIevelId(menuList.get(i).getMenuIevelId());
                 pwMenu2.setMenuIevelSign(menuList.get(i).getMenuIevelSign());
                 pwMenu2.setRoutingPath(menuList.get(i).getRoutingPath());
                 pwMenu2.setSubmenuId(menuList.get(i).getSubmenuId());
                 pwMenu2.setSubmenuSign(menuList.get(i).getSubmenuSign());
                 pwMenu2.setThreeLevelMenuId(menuList.get(i).getThreeLevelMenuId());
                 pwMenu2.setThreeLevelMenuSign(menuList.get(i).getThreeLevelMenuSign());
                 arrayList.add(pwMenu2);
             }
             if (id==5){
                 pwMenu3.setId(id);
                 pwMenu3.setMenuIevelId(menuList.get(i).getMenuIevelId());
                 pwMenu3.setMenuIevelSign(menuList.get(i).getMenuIevelSign());
                 pwMenu3.setRoutingPath(menuList.get(i).getRoutingPath());
                 pwMenu3.setSubmenuId(menuList.get(i).getSubmenuId());
                 pwMenu3.setSubmenuSign(menuList.get(i).getSubmenuSign());
                 pwMenu3.setThreeLevelMenuId(menuList.get(i).getThreeLevelMenuId());
                 pwMenu3.setThreeLevelMenuSign(menuList.get(i).getThreeLevelMenuSign());
                 arrayList.add(pwMenu3);
             }
             if (id==2){
                 totallist.setId(id);
                 totallist.setMenuIevelId(menuList.get(i).getMenuIevelId());
                 totallist.setMenuIevelSign(menuList.get(i).getMenuIevelSign());
                 totallist.setRoutingPath(menuList.get(i).getRoutingPath());
                 totallist.setSubmenuId(menuList.get(i).getSubmenuId());
                 totallist.setSubmenuSign(menuList.get(i).getSubmenuSign());
                 totallist.setThreeLevelMenuId(menuList.get(i).getThreeLevelMenuId());
                 totallist.setThreeLevelMenuSign(menuList.get(i).getThreeLevelMenuSign());
             }
         }
         menuList.remove(1);
         menuList.remove(1);
         menuList.remove(5);
         menuList.remove(5);
         totallist.setPwMenusList(arrayList);
         encapsulationMenu.put("menuList",menuList);
         encapsulationMenu.put("totalList",totallist);
         response.setData(encapsulationMenu);
        }else {
            menuList = pwMenuMapper.selectAlls(pwMenu);
            for (int i = 0; i < menuList.size(); i++) {
                if (menuList.get(i).getMenuIevelSign()!=null){
                    menuList.remove(i);
                }
            }
         response.setData(menuList);
        }
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }

}
